package com.knowledge.business.service.impl;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledge.business.domain.KnowledgeDocument;
import com.knowledge.business.domain.KnowledgeDocumentSegment;
import com.knowledge.business.service.IKnowledgeDocumentSegmentService;
import com.knowledge.business.service.IKnowledgeDocumentService;
import com.knowledge.common.AliyunOssUploadUtils;
import com.knowledge.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.knowledge.business.mapper.KnowledgeTypeMapper;
import com.knowledge.business.domain.KnowledgeType;
import com.knowledge.business.service.IKnowledgeTypeService;

/**
 * 知识库分类Service业务层处理
 *
 * @author zhaoyk
 * @date 2025-09-13
 */
@Service
public class KnowledgeTypeServiceImpl extends ServiceImpl<KnowledgeTypeMapper, KnowledgeType> implements IKnowledgeTypeService {
    @Autowired
    private KnowledgeTypeMapper knowledgeTypeMapper;

    /**
     * 查询知识库分类
     *
     * @param id 知识库分类主键
     * @return 知识库分类
     */
    @Override
    public KnowledgeType selectKnowledgeTypeById(Long id) {
        return knowledgeTypeMapper.selectKnowledgeTypeById(id);
    }

    /**
     * 查询知识库分类列表
     *
     * @param knowledgeType 知识库分类
     * @return 知识库分类
     */
    @Override
    public List<KnowledgeType> selectKnowledgeTypeList(KnowledgeType knowledgeType) {
        return knowledgeTypeMapper.selectKnowledgeTypeList(knowledgeType);
    }

    /**
     * 新增知识库分类
     *
     * @param knowledgeType 知识库分类
     * @return 结果
     */
    @Override
    public int insertKnowledgeType(KnowledgeType knowledgeType) {
        knowledgeType.setCreateTime(DateUtils.getNowDate());
        return knowledgeTypeMapper.insertKnowledgeType(knowledgeType);
    }

    /**
     * 修改知识库分类
     *
     * @param knowledgeType 知识库分类
     * @return 结果
     */
    @Override
    public int updateKnowledgeType(KnowledgeType knowledgeType) {
        knowledgeType.setUpdateTime(DateUtils.getNowDate());
        return knowledgeTypeMapper.updateKnowledgeType(knowledgeType);
    }

    /**
     * 批量删除知识库分类
     *
     * @param ids 需要删除的知识库分类主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeTypeByIds(Long[] ids) {
        return knowledgeTypeMapper.deleteKnowledgeTypeByIds(ids);
    }

    /**
     * 删除知识库分类信息
     *
     * @param id 知识库分类主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeTypeById(Long id) {
        return knowledgeTypeMapper.deleteKnowledgeTypeById(id);
    }


    @Autowired
    private IKnowledgeDocumentService knowledgeDocumentService;


    @Autowired
    private IKnowledgeDocumentSegmentService knowledgeDocumentSegmentService;

    @Autowired
    VectorStorageService storageService;

    @Autowired
    AliyunOssUploadUtils aliyunOssUploadUtils;

    @Override
    public void deleteSj(String[] ids) {
        QueryWrapper<KnowledgeDocument> knowledgeDocumentQueryWrapper = new QueryWrapper<>();
        knowledgeDocumentQueryWrapper.in("type_id", Arrays.asList(ids));
        List<KnowledgeDocument> documentlist = knowledgeDocumentService.list(knowledgeDocumentQueryWrapper);
        for (int i = 0; i < documentlist.size(); i++) {
            // 删除段落
            QueryWrapper<KnowledgeDocumentSegment> knowledgeDocumentSegmentQueryWrapper = new QueryWrapper<>();
            knowledgeDocumentSegmentQueryWrapper.eq("document_id", documentlist.get(i).getId());
            knowledgeDocumentSegmentService.remove(knowledgeDocumentSegmentQueryWrapper);
            // 删除文档
            knowledgeDocumentService.removeById(documentlist.get(i));
            // 删除向量数据库中的内容
            storageService.deleteDocumentVectors(documentlist.get(i).getId());
        }
        for (int i = 0; i < ids.length; i++) {
            // 删除阿里云文件
            aliyunOssUploadUtils.deleteFileByForder(ids[i]);
        }
        this.removeByIds(Arrays.asList(ids));
    }
}
